using UnityEngine;

public class FollowLuteBehaviour : MonoBehaviour
{
    private Rigidbody rb;
    private EnemyFlip enemyFlip;
    private AnimationController anim;
    private ActorSFX actorSFX;


    [Header("Target Settings")]
    [SerializeField] private Transform targetPosition;

    [Header("Movement Settings")]
    [SerializeField] private float moveSpeed = 1f;
    [SerializeField] private float animationLength = 2f;
    [SerializeField] private float animationDistance = 1f;

    [Header("Follow Settings")]
    [SerializeField] private float activationRadius = 5f;
    [SerializeField] private float stopDistance = 1.5f;

    [Header("Music Settings")]
    [SerializeField] AudioClip luteAmbientSound;

    private Transform player;
    private bool isFollowing = false;

    private void Awake()
    {
        rb = GetComponent<Rigidbody>();
        enemyFlip = GetComponent<EnemyFlip>();
        anim = GetComponent<AnimationController>();
        actorSFX = GetComponent<ActorSFX>();

    }

    private void Start()
    {
        GameObject playerObj = GameObject.FindGameObjectWithTag("Player");
        if (playerObj != null)
        {
            player = playerObj.transform;
        }
        if (actorSFX != null)
        {
            actorSFX.PlayLoopingSFX(luteAmbientSound);
        }
    }

    private void FixedUpdate()
    {
        CheckActivation();
        FollowTarget();
    }

    private void CheckActivation()
    {
        if (player == null || isFollowing)
            return;

        float distanceToPlayer = Vector3.Distance(transform.position, player.position);

        if (distanceToPlayer <= activationRadius)
        {
            isFollowing = true;
        }
    }

    private void FollowTarget()
    {
        if (targetPosition == null || !isFollowing)
        {
            if (anim != null)
            {
                anim.SetAnim("idle", moveSpeed, animationDistance, animationLength);
            }
            return;
        }

        Vector3 toTarget = targetPosition.position - transform.position;
        toTarget.y = 0f;

        float distanceToTarget = toTarget.magnitude;

        if (distanceToTarget > stopDistance)
        {
            Vector3 direction = toTarget.normalized;

            if (anim != null)
            {
                anim.SetAnim("walk", moveSpeed, animationDistance, animationLength);
            }

            if (enemyFlip != null)
            {
                enemyFlip.LookPlayer(direction);
            }

            if (rb != null)
            {
                rb.MovePosition(rb.position + direction * moveSpeed * Time.fixedDeltaTime);
            }
        }
        else
        {
            if (anim != null)
            {
                anim.SetAnim("idle", moveSpeed, animationDistance, animationLength);
            }
        }
    }

    private void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.yellow;
        Gizmos.DrawWireSphere(transform.position, activationRadius);

        Gizmos.color = Color.green;
        Gizmos.DrawWireSphere(transform.position, stopDistance);

        if (targetPosition != null)
        {
            Gizmos.color = Color.cyan;
            Gizmos.DrawLine(transform.position, targetPosition.position);
            Gizmos.DrawWireSphere(targetPosition.position, 0.5f);
        }
    }
}